package com.tencent.cloud.iov.processor.utils;

import java.util.Comparator;
import java.util.List;

import javax.lang.model.element.TypeElement;
import javax.lang.model.type.TypeMirror;

/**
 * 由父类至子类进行排序（父类 < 子类）
 *
 * @author showxiao
 * @date 2018/4/18
 */
public class TypeElementComparator implements java.util.Comparator<TypeElement> {
    @Override
    public int compare(TypeElement t1, TypeElement t2) {
        if (t1 == t2) {
            return 0;
        }

        List<? extends TypeMirror> interfaces = t1.getInterfaces();
        if (interfaces == null || interfaces.isEmpty()) {
            return -1;
        }

        Comparator<TypeMirror> typeMirrorComparator = new TypeMirrorComparator();
        for (TypeMirror typeMirror : interfaces) {
            if (typeMirrorComparator.compare(typeMirror, t2.asType()) >= 0) {
                return 1;
            }
        }

        return -1;
    }
}